package org.microdream.easy.deploy.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.microdream.easy.deploy.po.DServer;
import org.microdream.easy.deploy.po.DServerAuth;
import org.microdream.easy.deploy.service.DSequenceService;
import org.microdream.easy.deploy.service.DServerAuthService;
import org.microdream.easy.deploy.service.DServerService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ice.server.comm.BaseAction;
import ice.server.comm.GloubCode;
import ice.server.comm.ParamBulider;
import ice.server.comm.ResultPage;
import ice.tool.ResultMap;

/**
 * 服务器管理接口层
 * @author zhangrenbin
 *
 */
@RestController
@RequestMapping("/server")
public class DServerAction extends BaseAction {
	
	@Resource
	private DServerService dServerService;
	
	@Resource
    private DSequenceService dSequenceService;
	
	@Resource
	private DServerAuthService dServerAuthService;

	/**
	 * 分页查询
	 * @param param 查询条件
	 * @return
	 */
	@PostMapping("/page")
	public ResultPage<DServer> queryPage(DServer param) {
		// 构造查询条件
		Map<String, Object> map = ParamBulider.build().addContains("serverIp", param.getServerIp()).
			addContains("serverName", param.getServerName()).addPageAndRow(getPage(), getRows()).get();
		
		return pageToResultPage(dServerService.queryPage(map));
	}
	
	/**
	 * 新增
	 * @param server 服务器配置
	 * @return
	 */
	@PostMapping("")
	public Map<String, Object> add(DServer server) {
		server.setServerCode(dSequenceService.getNextSequence(GloubCode.SERVERCODE.getName(),GloubCode.SUPPLEMENTCODE.getName()));
		server.setCreateTime(new Date());
		server.setCreator(getMyAccount());
		server.setCreatorName(getMyName());
		return dServerService.add(server);
	}
	
	/**
	 * 删除服务器
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/{ids}")
	public Map<String, Object> delete(@PathVariable int[] ids) {
		return dServerService.deletServer(ids);
	}
	
	/**
	 * 查询服务器的用户
	 * @param code
	 * @return
	 */
	@GetMapping("/{code}/auth")
	public List<DServerAuth> getAuth(@PathVariable String code) {
		List<DServerAuth> al = dServerAuthService.find(ParamBulider.build().addEqual("serverCode", code).get());
		al.forEach(d -> d.setUserpwd(""));
		return al;
	}
	
	/**
	 * 添加用户
	 * @param code
	 * @return
	 */
	@PostMapping("/addAuth")
	public Map<String, Object> addAuth(DServerAuth auth) {
		return dServerAuthService.addAuth(auth);
	}
	
	/**
	 * 修改密码
	 * @param code
	 * @return
	 */
	@PutMapping("/modAuth")
	public Map<String, Object> modAuth(DServerAuth auth) {
		return dServerAuthService.modAuth(auth);
	}
	
	/**
	 * 查询服务器的用户
	 * @param code
	 * @return
	 */
	@DeleteMapping("/delAuth/id")
	public Map<String, Object> delAuth(@PathVariable int id) {
		return dServerAuthService.delAuth(id);
	}
}
